class UserSessionsController < ApplicationController

  def index
    if current_user.roles.detect { |role| role.id == 1}
      redirect_to :controller => 'users'
    else
      if current_user.roles.detect { |role| role.id == 2}
        redirect_to :controller => 'patient_datas'
      else
        if current_user.roles.detect { |role| role.id == 3}
          redirect_to :controller => 'order_examinations_for_technik'
        else
          if current_user.roles.detect { |role| role.id == 4}
            redirect_to :controller => 'order_examinations_for_technik'
          else
            redirect_to  :controller => "user_sessions", :action => "new"
          end
        end
      end
    end
  end


  def new
    @user_session = UserSession.new
  end

  def create
    @user_session = UserSession.new(params[:user_session])
    if @user_session.save
      flash[:notice] = "Prawidłowo zalogowany"
      
      if current_user.roles.detect { |role| role.id == 1}
        redirect_to :controller => 'users'
      else
        if current_user.roles.detect { |role| role.id == 2}
          redirect_to :controller => 'patient_datas'
        else
          if current_user.roles.detect { |role| role.id == 3}
            redirect_to :controller => 'order_examinations_for_technik'
          else
            if current_user.roles.detect { |role| role.id == 4}
              redirect_to :controller => 'order_examinations_for_technik'
            else
              redirect_to  :controller => "user_sessions", :action => "new"
            end
          end
        end
      end

    else
      render :action => 'new'
    end
  end

  def destroy
    @user_session = UserSession.find(params[:id])
    @user_session.destroy
    flash[:notice] = "Prawidłowo wylogowany"
    redirect_to root_url
  end
end
